﻿using System.IO;


namespace Mga
{
    public static class CFileUtility
    {
        #region Exist
        public static bool Exist(string fileName)
        {

            return File.Exists(fileName);
        }

        public static void Copy(string scr, string target)
        {
            File.Copy(scr, target);
        }
        #endregion

        #region GetFiles
        public static string GetFileName(string path, bool isRemoveExtension = true)
        {
            string fileName = string.Empty;
            if (isRemoveExtension)
                fileName = Path.GetFileNameWithoutExtension(path);
            else
                fileName = Path.GetFileName(path);
            return fileName;
        }
        public static string[] GetFileNames(string directoryPath, bool isRemoveExtension = true)
        {
            string[] files = Directory.GetFiles(directoryPath);
            for (int i = 0; i < files.Length; i++)
            {
                if (isRemoveExtension)
                    files[i] = Path.GetFileNameWithoutExtension(files[i]);
                else
                    files[i] = Path.GetFileName(files[i]);
            }
            return files;
        }
        #endregion

        #region Remove File
        public static void RemoveFile(string name)
        {
            try
            {
                File.Delete(name);
            }
            catch (System.Exception ex)
            {
                ggc.Foundation.Log.LogErrorMsg(ex.ToString());
            }
        }
        #endregion

        #region Read/Write String
        public static bool WriteToFile(string name, string content)
        {
            try
            {

                FileStream fs = new FileStream(name, FileMode.Create);
                StreamWriter writer = new StreamWriter(fs);
                writer.Write(content);
                writer.Close();
                fs.Close();
                return true;
            }
            catch (System.Exception ex)
            {
                ggc.Foundation.Log.LogErrorMsg("Fail to WriteFile:" + name);
                ggc.Foundation.Log.LogErrorMsg(ex.ToString());
                return false;
            }
        }

        public static bool ReadFromFile(string name, ref string content)
        {
            try
            {
                if (!File.Exists(name))
                    return false;
                FileStream fs = new FileStream(name, FileMode.Open);
                StreamReader reader = new StreamReader(fs);
                content = reader.ReadToEnd();
                reader.Close();
                fs.Close();
                return true;
            }
            catch (System.Exception ex)
            {
                ggc.Foundation.Log.LogErrorMsg("Fail to ReadFromFile:" + name);
                ggc.Foundation.Log.LogErrorMsg(ex.ToString());
                return false;
            }
        }
        #endregion

        #region Read/Write Bytes
        public static bool WriteToFile(string name, byte[] content, int offset, int count)
        {
            try
            {
                FileStream fs = new FileStream(name, FileMode.Create);
                BinaryWriter writer = new BinaryWriter(fs);
                writer.Write(content, offset, count);
                writer.Close();
                fs.Close();
                return true;
            }
            catch (System.Exception ex)
            {
                ggc.Foundation.Log.LogErrorMsg("Fail to WriteFile:" + name);
                ggc.Foundation.Log.LogErrorMsg(ex.ToString());
                return false;
            }
        }

        public static bool ReadFromFile(string name, out byte[] content)
        {
            content = null;
            try
            {
                if (!File.Exists(name))
                    return false;
                FileStream fs = new FileStream(name, FileMode.Open);
                BinaryReader reader = new BinaryReader(fs);
                content = reader.ReadBytes((int)reader.BaseStream.Length);
                reader.Close();
                fs.Close();
                return true;
            }
            catch (System.Exception ex)
            {
                ggc.Foundation.Log.LogErrorMsg("Fail to ReadFromFile:" + name);
                ggc.Foundation.Log.LogErrorMsg(ex.ToString());
                return false;
            }
        }
        #endregion
    }
}
